import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import axios from "axios"
import "@/assets/global.css"

Vue.config.productionTip = false
//请求拦截器
axios.interceptors.request.use(config => {
    var token = sessionStorage.getItem("token");
    if (token) {
        config.headers.token = token;
    }
    return config;
})
axios.defaults.baseURL = "http://localhost:81"
Vue.prototype.$http = axios;
router.beforeEach((to,from,next)=>{
    //如果用户访问的是登录页面，直接放行 ===比较值和引用
    if(to.path === "/login"){
        //放行
        return next();
    }
    //获取sessionStorage中保存的token值
    const token = window.sessionStorage.getItem("token");
    //如果token不存在，强制跳转到登录页面
    if(!token){
        return next("/login");
    }
    //如果token存在，直接放行
    next();
})

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')
